package main.java.indi.zyj.hot100;

public class _06_trap {
    public int trap(int[] height) {
        int ans = 0, left = 0, right = height.length - 1, preMax = 0, sufMax = 0;
        while (left < right) {
            preMax = Math.max(preMax, height[left]);
            sufMax = Math.max(sufMax, height[right]);
            ans += preMax < sufMax ? preMax - height[left++] : sufMax - height[right--];
        }
        return ans;
    }

    public int trap1(int[] height) {
        int left = 0, right = height.length - 1, res = 0, leftMax = 0, rightMax = 0;
        while (left < right) {
            leftMax = Math.max(height[left], leftMax);
            rightMax = Math.max(height[right], rightMax);
            res += leftMax > rightMax ? rightMax - height[right--] : leftMax - height[left++];
        }
        return res;
    }
}
